//
//  BKTextView.m
//  XPhoto
//
//  Created by baboy on 2019/7/10.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKTextView.h"
@interface BKTextView()
@property(nonatomic, strong) NSArray *defaultGestureRecognizers;
@end
@implementation BKTextView
- (void) setEnableCustomGestureRecognizers:(BOOL)enableCustomGestureRecognizers{
    _enableCustomGestureRecognizers = enableCustomGestureRecognizers;
    if (enableCustomGestureRecognizers) {
        self.defaultGestureRecognizers = self.gestureRecognizers;
        self.gestureRecognizers = nil;
        
        UITapGestureRecognizer *singleTap = [UITapGestureRecognizer new];
        singleTap.numberOfTapsRequired = 1;
        [singleTap addTarget:self action:@selector(tapped:)];
        [self addGestureRecognizer:singleTap];
        
        UILongPressGestureRecognizer *longTap = [UILongPressGestureRecognizer new];
        longTap.numberOfTouchesRequired = 1;
        [longTap addTarget:self action:@selector(longTapped:)];
        [self addGestureRecognizer:longTap];
        [singleTap requireGestureRecognizerToFail:longTap];
    }
    if (!enableCustomGestureRecognizers) {
        if (self.defaultGestureRecognizers) {
            self.gestureRecognizers = self.defaultGestureRecognizers;
        }
    }
}
- (void)tapped:(UITapGestureRecognizer *)r{
    if (r.state == UIGestureRecognizerStateEnded) {
        CGPoint p = [r locationInView:self];
        NSInteger i = [self.layoutManager glyphIndexForPoint:p inTextContainer:self.textContainer];
        NSRange textRange;
        NSDictionary *attr = [self.textStorage attributesAtIndex:i effectiveRange:&textRange];
        if (attr[NSLinkAttributeName]) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(textView:shouldInteractWithURL:inRange:interaction:)]) {
                NSURL *URL = [NSURL URLWithString:attr[NSLinkAttributeName]];
                [self.delegate textView:self shouldInteractWithURL:URL inRange:textRange interaction:UITextItemInteractionInvokeDefaultAction];
            }
        }
    }
}
- (void)longTapped:(UITapGestureRecognizer *)r{
    if (r.state == UIGestureRecognizerStateEnded) {
        CGPoint p = [r locationInView:self];
        NSInteger i = [self.layoutManager glyphIndexForPoint:p inTextContainer:self.textContainer];
        NSRange textRange;
        NSDictionary *attr = [self.textStorage attributesAtIndex:i effectiveRange:&textRange];
        if (attr[NSLinkAttributeName]) {
            id delegate = self.delegate;
            if (delegate && [delegate respondsToSelector:@selector(textView:shouldInteractWithURL:inRange:forGestureRecognizer:)]) {
                NSURL *URL = [NSURL URLWithString:attr[NSLinkAttributeName]];
                [delegate textView:self shouldInteractWithURL:URL inRange:textRange forGestureRecognizer:r];
            }
        }
    }
}
-(void)test:(UIView *)v{
    if ([NSStringFromClass([v class]) isEqualToString:@"UITextSelectionView"] ) {
        v.hidden = YES;
    }
    for (UIView *v1 in v.subviews) {
        [self test:v1];
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
